<template>
  <div class="system-login-log-container standard-container layout-padding layout-pd">
    <div class="review-list-title">
      <div class="title-actions">
        <!-- <el-input v-model="state.search.name" size="default" clearable placeholder="请输入应用名称" style="max-width: 180px"> </el-input> -->
        <el-button size="default" type="primary" class="ml10" @click="getTableData">
          <el-icon>
            <ele-Search/>
          </el-icon>
          查询
        </el-button>
        <el-button size="default" type="primary" class="ml10" @click="onOpen('add', '')">
          <el-icon>
            <ele-FolderAdd/>
          </el-icon>
          新增应用
        </el-button>
      </div>
    </div>
    <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
      <el-table-column prop="userId" label="用户ID" show-overflow-tooltip></el-table-column>
      <el-table-column prop="username" label="用户名" show-overflow-tooltip></el-table-column>
      <el-table-column prop="ip" label="客户端ip" show-overflow-tooltip></el-table-column>
      <el-table-column prop="loginTime" label="登录时间" show-overflow-tooltip></el-table-column>
      <el-table-column prop="platform" label="登录平台" show-overflow-tooltip></el-table-column>
      <el-table-column prop="userAgent" label="userAgent" show-overflow-tooltip width="400"></el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button size="small" text type="primary" @click="onOpen(scope.row.id)">查看</el-button>
          <el-button size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          :page-sizes="[10, 15, 20, 30, 50, 100]"
          v-model:current-page="state.tableData.paging.currentPage"
          background
          v-model:page-size="state.tableData.paging.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.paging.totalCount"
      >
      </el-pagination>
    </div>
    <Dialog ref="DialogRef"></Dialog>
  </div>
</template>

<script setup lang="ts">
import {LoginLogApi} from '/@/api/log/login/index';
import {defineAsyncComponent, onMounted, reactive, ref} from 'vue';

import msg from '/@/utils/messageUtils';
import {ElMessage, ElMessageBox} from 'element-plus';

const Dialog = defineAsyncComponent(() => import('./dialog.vue'));
const DialogRef = ref();
const state = reactive({
  isToggle: false,
  search: {
    name: '',
  },
  tableData: {
    data: [],
    total: 0,
    loading: false,
    paging: {
      currentPage: 1,
      pageSize: 15,
      totalCount: 0,
      totalPages: 0,
    },
  },
});

const getTableData = async () => {
  try {
    const {data} = await LoginLogApi().search({
      offset: (state.tableData.paging.currentPage - 1) * state.tableData.paging.pageSize,
      limit: state.tableData.paging.pageSize,
    });
    state.tableData.data = data.datas;
    state.tableData.paging.currentPage = data.paging.currentPage;
    state.tableData.paging.totalCount = data.paging.totalCount;
    console.log('result: ', data);
  } catch (e) {
    msg().error('查询失败，请稍后重试');
  }
};
// 分页改变
const onHandleSizeChange = (pageSize: number) => {
  state.tableData.paging.pageSize = pageSize;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (currentPage: number) => {
  state.tableData.paging.currentPage = currentPage;
  getTableData();
};

const onRowDel = (row: any) => {
  ElMessageBox.confirm(`此操作将永久删除当前登录日志：是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        await LoginLogApi().delete(row.id);
        getTableData();
        ElMessage.success('删除成功');
      })
      .catch(() => {
      });
};

const onOpen = (id: string) => {
  DialogRef.value.openDialog(id);
};
onMounted(() => {
  getTableData();
});
</script>

<style scoped lang="scss">
.system-login-log-container {
  .el-table {
    flex: 1;
  }
}
</style>
